package com.cly.item.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cly.common.entity.category.dto.CategoryDTO;
import com.cly.common.entity.category.po.Category;
import com.cly.common.entity.page.PageBean;
import com.cly.common.entity.result.Result;
import com.cly.common.exception.CommonException;
import com.cly.item.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 分页查询分类
     * @param pageNum
     * @param pageSize
     * @param categoryName
     * @return
     */
    @GetMapping("/public/queryPage")
    public Result queryCategoryByPage(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "name", required = false) String categoryName
    ) {
        try {
            Page page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(categoryName), Category::getCategoryName, categoryName);
            Page pageCategory = categoryService.page(page, queryWrapper);
            return Result.success(new PageBean(pageCategory.getSize(), pageCategory.getRecords()));
        } catch (Exception e) {
            log.error("查询分类失败");
            throw new CommonException("查询分类失败",e);
        }
    }

    /**
     * 查询所有分类
     * @return
     */
    @GetMapping("/public/list")
    public Result getCategoryAll() {
        return categoryService.getCategoryAll();
    }

    /**
     * 添加分类
     * @param categoryDTO
     * @return
     */
    @PostMapping("/add")
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        try {
            categoryService.save(BeanUtil.copyProperties(categoryDTO, Category.class));
            categoryService.deleteCategoryRedis(); // 删除redis缓存
            return Result.success();
        } catch (Exception e) {
            throw new CommonException("添加商品分类失败",e);
        }
    }

    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping("/update")
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        try {
            categoryService.updateById(BeanUtil.copyProperties(categoryDTO,Category.class));
            categoryService.deleteCategoryRedis(); // 删除redis缓存
            return Result.success();
        } catch (Exception e) {
            throw new CommonException("修改商品分类失败",e);
        }
    }

    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteCategory(@PathVariable("id") Long id) {
        try {
            categoryService.removeById(id);
            categoryService.deleteCategoryRedis(); // 删除redis缓存
            return Result.success();
        } catch (Exception e) {
            throw new CommonException("删除商品分类失败",e);
        }
    }

}
